package com.ellwood.copyright.common.core.base;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ellwood.copyright.common.core.result.pojo.EllQuery;

/**
 * @ProjectName: ell-copyright-pro
 * @PackageName: com.ellwood.copyright.common.core.base
 * @ClassName: ServiceImplFactory
 * @Description: // service接口实现基类
 * @Author: Ellwood
 * @CreateDate: 2024/11/11 - 13:31
 * @Version: V1.0.0
 */
public class ServiceImplFactory<M extends BaseMapper<T>,T> extends ServiceImpl<M,T> implements ServiceFactory<T>{

    /**
     * 获取分页对象
     * @param query 分页参数
     * @return 返回分有对象
     */
    public IPage<T> getPage(EllQuery query) {
        Page<T> page = new Page<>(query.getPage(), query.getLimit());
        // 排序
        if (StringUtils.isNotBlank(query.getOrder())) {
            if (query.isAsc()) {
                return page.addOrder(OrderItem.asc(query.getOrder()));
            }else {
                return page.addOrder(OrderItem.desc(query.getOrder()));
            }
        }
        return page;
    }

}
